package cliente;

import java.io.File;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderJDOMFactory;
import org.jdom2.input.sax.XMLReaderXSDFactory;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import deposito.dto.SolicitudArticulo;
import deposito.facade.IFACADE;


public class ClienteTEST {
	
	public static void main(String args[]) throws Exception{
		
		//parametros
		Properties jndiProps = new Properties();
		jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
		jndiProps.put(Context.PROVIDER_URL,"remote://127.0.0.1:4447");

		 // This is an important property to set if you want to do EJB invocations via the remote-naming project
		 jndiProps.put("jboss.naming.client.ejb.context", true);
		 
		 // create a context passing these properties
		 Context context = new InitialContext(jndiProps);
		 
		 // lookup the bean
		 IFACADE iDeposito =(IFACADE)context.lookup("TPO_IA_Deposito1_EAR/TPO_IA_Deposito1/FACADEService!deposito.facade.IFACADE");
		 
		 //***** PRUEBAS *****//
		 
		 // SOLICITUDES ARTICULO //

		 //AGREGAR SOLICITUD ARTICULO
		 
		 String path = "C:\\IA_REPO\\SolicitudArticulo.xml";
		 String path2 = "C:\\IA_REPO\\SolicitudArticulo2.xml";
		 String pathXSD = "C:\\IA_REPO\\solicitudArticuloADepositoXML-schema.xsd";
		 
		 File xsdfile = new File(pathXSD);
		 XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(xsdfile);
		 SAXBuilder builder = new SAXBuilder(schemafac);
		 File xmlfile = new File(path);
		 Document validdoc = (Document) builder.build(xmlfile);
		 
		 
		 File xsdfile2 = new File(pathXSD);
		 XMLReaderJDOMFactory schemafac2 = new XMLReaderXSDFactory(xsdfile2);
		 SAXBuilder builder2 = new SAXBuilder(schemafac2);
		 File xmlfile2 = new File(path2);
		 Document validdoc2 = (Document) builder2.build(xmlfile2);
		 
		 
		 String xml = convertXMLtoString(validdoc);
		 String xml2 = convertXMLtoString(validdoc2);
		 
		 iDeposito.pruebaJMS(xml,2);
		 iDeposito.pruebaJMS(xml2,2);
		 
		 
		 List<SolicitudArticulo> listaSolicitudes = iDeposito.listarSolicitudesPendientes();
		 
		 SolicitudArticulo sol = iDeposito.buscarSolicitudArticulo(listaSolicitudes.get(0).getId());

		 System.out.println(sol.getId());
	}
	
	 /*private static String convertDocumentToString(Document doc) {
	        TransformerFactory tf = TransformerFactory.newInstance();
	        Transformer transformer;
	        try {
	            transformer = tf.newTransformer();
	            StringWriter writer = new StringWriter();
	            transformer.transform(new DOMSource(doc), new StreamResult(writer));
	            String output = writer.getBuffer().toString();
	            return output;
	        } catch (TransformerException e) {
	            e.printStackTrace();
	        }
	         
	        return null;
	    }
	    */
	
	
	    private static String convertXMLtoString(Document archivo) {
	        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
	        String xmlString = outputter.outputString(archivo);
	        System.out.println(xmlString);
	        return xmlString;
	    }
	
}
